package com.lagou.work.task03.angel.jobService.clientService;

import com.lagou.work.task03.angel.job.impl.JobServiceImpl;
import com.lagou.work.task03.angel.utils.ParsePropertiesFileUtil;

import java.net.Socket;
import java.util.Map;

/**
 * @author：dingyuan
 * @date：2022.4.1 19:46
 * @description：模拟分布式计算-客户端功能抽象类
 */
public abstract class JobClientBase {
    private static final String CLIENT_CONFIG_NAME = "angle/client-";
    private static final String FILE_TYPE = ".properties";
    private String host;
    private int port;

    /**
     * @description：客户端入口
     * @param args
     * @throws Exception
     */
    public void run(String[] args) throws Exception {
        JobServiceImpl jobService = new JobServiceImpl();
        loadClientConfig(args);
        Socket jobClient = jobService.getJobClient(host, port);
        handleJob(jobClient);
    }

    /**
     * @description：加载客户端配置文件
     * @param args
     * @throws Exception
     */
    public void loadClientConfig(String[] args) throws Exception {
        if("SIT".equals(args[0])){
            Map<String, String> configMap = new ParsePropertiesFileUtil().readProperties(CLIENT_CONFIG_NAME+"sit"+FILE_TYPE);
            this.host = configMap.get("client.host");
            this.port = Integer.valueOf(configMap.get("client.port")) ;
        }else {
           throw new Exception("The input env param{} is error, please check");
        }
    }
    /**
     * @description：客户端处理逻辑
     * @param jobClient
     * @throws Exception
     */
    public abstract void handleJob(Socket jobClient) throws Exception;
}
